package com.geoserver.service;

import com.geoserver.utils.ImageCompress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * FileName: ImageService
 * Create by fengzheng on 2019/4/26 10:32
 * Description: 图片服务类
 */

@Service
public class ImageService {

    private ImageCompress imageCompress;

    @Autowired
    public ImageService(ImageCompress imageCompress) {
        this.imageCompress = imageCompress;
    }

    /**
     * 获取图片流
     * @param addr 图片地址
     * @param type 压缩类型 1-压缩图片，2-不压缩图片
     */
    public BufferedImage getImageStream(String addr,int type){
        final String imageHeadPath = "F:\\Files\\01第一天照片\\";
        String filePath = imageHeadPath + addr;

        File file = new File(filePath);
        if(file.exists()){
            if( type == 1){
                try {
                    Image sImage = ImageIO.read(new FileInputStream(filePath));
                    return imageCompress.resizeFix(sImage);
                } catch (Exception e) {
                    e.printStackTrace();

                    return null;
                }
            }else{
                try {
                    return ImageIO.read(new FileInputStream(filePath));
                } catch (IOException e) {
                    e.printStackTrace();

                    return null;
                }
            }
        }else{
            return null;
        }
    }
}
